package com.ruoyi.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.system.mapper.HatchetPlanItemMapper;
import com.ruoyi.system.mapper.HatchetPlanMapper;
import com.ruoyi.system.domain.HatchetPlan;
import com.ruoyi.system.domain.HatchetPlanItem;
import com.ruoyi.system.service.IHatchetPlanItemService;
import com.ruoyi.common.enums.BusinessStatus;
import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.StringUtils;

/**
 * 还款计划明细 服务层实现
 * 
 * @author ruoyi
 * @date 2019-03-02
 */
@Service
public class HatchetPlanItemServiceImpl implements IHatchetPlanItemService 
{
	@Autowired
	private HatchetPlanItemMapper hatchetPlanItemMapper;
	@Autowired
	private HatchetPlanMapper hatchetPlanMapper;

	/**
     * 查询还款计划明细信息
     * 
     * @param iD 还款计划明细ID
     * @return 还款计划明细信息
     */
    @Override
	public HatchetPlanItem selectHatchetPlanItemById(String iD)
	{
	    return hatchetPlanItemMapper.selectHatchetPlanItemById(iD);
	}
	
	/**
     * 查询还款计划明细列表
     * 
     * @param hatchetPlanItem 还款计划明细信息
     * @return 还款计划明细集合
     */
	@Override
	public List<HatchetPlanItem> selectHatchetPlanItemList(HatchetPlanItem hatchetPlanItem)
	{
	    return hatchetPlanItemMapper.selectHatchetPlanItemList(hatchetPlanItem);
	}
	
    /**
     * 新增还款计划明细
     * 
     * @param hatchetPlanItem 还款计划明细信息
     * @return 结果
     */
	@Override
	public int insertHatchetPlanItem(HatchetPlanItem hatchetPlanItem)
	{
	    return hatchetPlanItemMapper.insertHatchetPlanItem(hatchetPlanItem);
	}
	
	/**
     * 修改还款计划明细
     * 
     * @param hatchetPlanItem 还款计划明细信息
     * @return 结果
     */
	@Override
	public int updateHatchetPlanItem(HatchetPlanItem hatchetPlanItem)
	{
	    return hatchetPlanItemMapper.updateHatchetPlanItem(hatchetPlanItem);
	}

	/**
     * 删除还款计划明细对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteHatchetPlanItemByIds(String ids)
	{
		return hatchetPlanItemMapper.deleteHatchetPlanItemByIds(Convert.toStrArray(ids));
	}

	/**
	 * 代理商查询还款计划明细列表
	 * @param hatchetPlanItem
	 * @return
	 */
	public List<HatchetPlanItem> selectHatchetPlanItemListForDLS(HatchetPlanItem hatchetPlanItem) {
		return hatchetPlanItemMapper.selectHatchetPlanItemListForDLS(hatchetPlanItem);
	}

	/**
	 * 客服操作完成计划
	 * @param planItemId
	 * @return
	 */
	@Transactional
	public Map<String, Object> completePlanItem(String planItemId) {
		Map<String, Object> map = new HashMap<>();
		HatchetPlanItem item = selectHatchetPlanItemById(planItemId);
		if (StringUtils.isNull(item)) {
			map.put("msg", "子计划无法查到");
			map.put("status", BusinessStatus.FAIL);
			return map;
		}
		item.setSTATUS("10B");
		int updateHatchetPlanItem = updateHatchetPlanItem(item);
		if (updateHatchetPlanItem > 0) {
			//判断大计划下面的小计划是否全部完成
			//查询大计划下面的小计划数量
			HatchetPlan plan = hatchetPlanMapper.selectHatchetPlanById(item.getPlanId());
			HatchetPlanItem paramItem = new HatchetPlanItem();
			paramItem.setPlanId(plan.getID());
			//查询大计划下面全部小计划
			List<HatchetPlanItem> allPlanItemList = selectHatchetPlanItemList(paramItem);
			//查询大计划下面已完成的小计划
			paramItem.setSTATUS("10B");
			List<HatchetPlanItem> completePlanItemList = selectHatchetPlanItemList(paramItem);
			//判断小计划是否全部完成
			if (allPlanItemList.size()==completePlanItemList.size()) {
				//修改大计划状态为完成
				plan.setSTATUS("10E");
				int updateHatchetPlan = hatchetPlanMapper.updateHatchetPlan(plan);
				if (updateHatchetPlan<1) {
					throw new RuntimeException("大计划完成失败,请重新完成小计划");
				}
			}
			map.put("status", BusinessStatus.SUCCESS);
			map.put("msg", "成功完成计划");
			return map;
		} else {
			map.put("msg", "计划完成失败，请重新完成计划！");
			map.put("status", BusinessStatus.FAIL);
			return map;
		}
	}

	/**
	 * 高级顾问查询计划明细列表
	 * @param hatchetPlanItem
	 * @return
	 */
	public List<HatchetPlanItem> selectHatchetPlanItemListForJLR(HatchetPlanItem hatchetPlanItem) {
		
		return hatchetPlanItemMapper.selectHatchetPlanItemListForJLR(hatchetPlanItem);
	}
	
}
